package me.green.ui.global;

import java.awt.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 本地字体集
 *
 * @author green
 */
public class LocalFonts {

    private int defaultFontSize = 18;
    private float fDefaultFontSize = defaultFontSize;
    private Map<String, Font> localFonts;

    /**
     * 方正宋体
     */
    public static final String FANGZHENGSONG = "FangZhengDaBiaoSongJianTi-1";
    public static final String YAHEI = "Microsoft YaHei";

    private LocalFonts() {
        localFonts = new HashMap<String, Font>();
        this.init();
    }

    /**
     * 本地字体初始化加载
     */
    private void init() {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            inputStream = LocalFonts.class.getResourceAsStream("/fonts/FangZhengDaBiaoSongJianTi-1.ttf");
            bufferedInputStream = new BufferedInputStream(inputStream);
            Font font = Font.createFont(Font.TRUETYPE_FONT, bufferedInputStream);
            // 需使用deriveFont(float size)函数， int类型参数不是指定字体大小
            localFonts.put(FANGZHENGSONG, font.deriveFont(fDefaultFontSize));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bufferedInputStream) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        localFonts.put(YAHEI, new Font("微软雅黑", Font.PLAIN, defaultFontSize));
    }

    /**
     * ui默认字体
     *
     * @return Font
     */
    public Font getDefaultFont() {
        return localFonts.get(YAHEI);
    }

    /**
     * 标题字体
     *
     * @return
     */
    public Font getTitleFont(float fontSize) {
        return localFonts.get(FANGZHENGSONG).deriveFont(fontSize);
    }

    static class Instant {
        static LocalFonts lf = new LocalFonts();

        public static LocalFonts get() {
            return lf;
        }
    }
}
